package com.spring.careclassapi.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spera.genutil.SpUtil;
import com.spera.genutil.TokenUtil;
import com.spring.careclassapi.web.dao.CareClassApiMapper;
import com.spring.careclassapi.web.model.CareClass;
import com.spring.careclassapi.web.model.CareClassOpen;
import com.spring.careclassapi.web.model.CareClassType;
import com.spring.careclassapi.web.model.Course;
import com.spring.careclassapi.web.service.CareClassApiService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.tools.MyTools;

@Service
public class CareClassApiServiceImpl implements CareClassApiService{
	@Resource
	private CareClassApiMapper careCourseApiMapper;
	
	@Resource
	private JedisCache jedisCache;
	
	
	//课程科目类型集合 
	@Override
	public Map<String, Object> getCareClassTypeList() throws Exception {
		Map<String, Object> m = new HashMap();
		
		/*Map<String, Object> t = checkToken(token);
		if (t != null) {

			return t;

		}

		String c_user_id = redisCache.lindex(token, 0);*/
		
		
			/*
			HashMap hmc=MyTools.checkIsNotNull(rechargecard_id);
			if(ApplicationUtils.isNotNull(hmc)){
				return hmc;
			}
			
			
			HashMap hm=new HashMap();
			hm.put("card_id", rechargecard_id);
			hm.put("c_family_member_id", c_family_member_id); SpUtil*/
			
			List<CareClassType> list = careCourseApiMapper.getCareClassTypeList();
			
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

		
		return m;
	}
	
	
		public String toNull(String para){
			
			if(para!=null){
				if(para.equals("")){
					para=null;
				}
			}
			
			
			return para;
		}
	
	
		//课程科目类型集合 
		@Override
		public Map<String, Object> getCareClassOpenList(String care_class_type_id, String kids_park_id,
				String start_time, String end_time, String care_class_open_id, String page_size, 
				String page_index, String skip, String rows) throws Exception {
			Map<String, Object> m = new HashMap();
			
				
			
				care_class_type_id=	this.toNull(care_class_type_id);
				kids_park_id=	this.toNull(kids_park_id);
				start_time=	this.toNull(start_time);
				end_time=	this.toNull(end_time);
				care_class_open_id=	this.toNull(care_class_open_id);
				
			
				HashMap hm=MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
				hm.put("care_class_type_id", care_class_type_id);
				hm.put("kids_park_id", kids_park_id);
				hm.put("start_time", start_time);
				hm.put("end_time", end_time);
				hm.put("care_class_open_id", care_class_open_id);
				
				
				
				
				List<CareClassOpen> list = careCourseApiMapper.getCareCourseOpenList(hm);
				
				m.put(ApplicationUtils.DATASET, list);
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "查询成功");

			
			return m;
		}

		
		
		
		
		//课程科目类型集合 
		@Override
		public Map<String, Object> getMyCareCourseOpenList(String token,String c_user_id,String care_class_type_id, String kids_park_id,
				String start_time, String end_time, String care_class_open_id, String page_size, 
				String page_index, String skip, String rows) throws Exception {
			Map<String, Object> m = new HashMap();
			
			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}
			
				care_class_type_id=	this.toNull(care_class_type_id);
				kids_park_id=	this.toNull(kids_park_id);
				start_time=	this.toNull(start_time);
				end_time=	this.toNull(end_time);
				care_class_open_id=	this.toNull(care_class_open_id);
				
			
				HashMap hm=MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
				hm.put("care_class_type_id", care_class_type_id);
				hm.put("kids_park_id", kids_park_id);
				hm.put("start_time", start_time);
				hm.put("end_time", end_time);
				hm.put("care_class_open_id", care_class_open_id);
				hm.put("c_user_id", c_user_id);
				
				
				
				List<HashMap<String,Object>> list = careCourseApiMapper.getMyCareCourseOpenList(hm);
				
				m.put(ApplicationUtils.DATASET, list);
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "查询成功");

			
			return m;
		}
		
		
		
	// 得到托班具体课程
	@Override
	public Map<String, Object> getCourseList(String care_class_open_id)
			throws Exception {
		Map<String, Object> m = new HashMap();

		HashMap hm = new HashMap();

		hm.put("care_class_open_id", care_class_open_id);

		List<Course> list = careCourseApiMapper.getCourseList(hm);

		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}

	/**
	 * 预约托班
	 * 
	 * @param token
	 * @param c_user_id
	 * @param user_name
	 * @param care_class_open_id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> appointCareClass(String token, String c_user_id,
			String user_name, String care_class_open_id) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {

			return t;

		}

		HashMap hmc = MyTools.checkIsNotNull(c_user_id, user_name,
				care_class_open_id);
		if (hmc != null) {
			return hmc;
		}

		HashMap hm = new HashMap();

		hm.put("c_user_id", c_user_id);
		hm.put("user_name", user_name);
		hm.put("care_class_open_id", care_class_open_id);
		
		
		int count = careCourseApiMapper.appointCareClassCheckId(hm);
		if(count!=1){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "请输入有效的id");
			return m;
		}
		int count2 =careCourseApiMapper.appointCareClassCheckDuplicate(hm);
		if(count2>=1){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "不要重复预约");
			return m;
		}
		
		int effect =careCourseApiMapper.appointCareClass(hm);
		
		if(effect==1){
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "插入成功");
		}else{
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "插入失败");
		}
		
		

		return m;
	}
	/**
	 * 取消预约
	 */
	@Override
	public Map<String, Object> cancelCareClass(String token,String c_user_id,
			String care_class_appointment_id) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {

			return t;

		}

		HashMap hmc = MyTools.checkIsNotNull(care_class_appointment_id);
		if (hmc != null) {
			return hmc;
		}

		HashMap hm = new HashMap();

		hm.put("c_user_id", c_user_id);
		hm.put("care_class_appointment_id", care_class_appointment_id);
		
		
		int count = careCourseApiMapper.cancelCareClassCheckId(hm);
		if(count!=1){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "请输入有效的id");
			return m;
		}
		int effect =careCourseApiMapper.cancelCareClass(hm);
		
		if(effect==1){
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "更新成功");
		}else{
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "插入失败");
		}
		
		

		return m;
	}
	
	/**
	 * 查看自己的预约/签到记录
	 */
	@Override
	public Map<String, Object> getMyCareClassList(String token,
			String c_user_id, String isattended, String start_time,
			String end_time, String page_size, String page_index, String skip,
			String rows) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {

			return t;

		}

		HashMap hmc = MyTools.checkIsNotNull(c_user_id);
		if (hmc != null) {
			return hmc;
		}

		HashMap hm = MyTools.pageIndexToSkip(page_size, page_index, skip, rows);

		hm.put("c_user_id", c_user_id);
		hm.put("isattended", isattended);
		hm.put("start_time", start_time);
		hm.put("end_time", end_time);
		
		
		List<HashMap<String,Object>> list=careCourseApiMapper.getMyCareCourseOpenList(hm);
	//	List<CareClass> list =careCourseApiMapper.getMyCareCourseOpenList(hm);
//scareCourseApiMapper.getMyCareClassOpenList(hm);

		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}
	
	
	
	@Override
	public Map<String, Object> getTodayCareClass(String kids_parkID) throws Exception {
		Map<String, Object> m = new HashMap();

		HashMap hm = new HashMap();
		
		HashMap hmc =MyTools.checkIsNotNull(kids_parkID);
		if(hmc!=null){
			return hmc;
		}

		hm.put("kids_parkID", kids_parkID);

		List<HashMap<String,Object>> list = careCourseApiMapper.getTodayCareClass(hm);

		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}

}
